package com.danawa.watcher.front;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.danawa.watcher.util.JChartUtil;
import com.danawa.watcherApi.database.command.common.Condition;
import com.danawa.watcherApi.database.command.common.Operator;
import com.danawa.watcherApi.database.domain.BaseProduct;
import com.danawa.watcherApi.database.domain.PriceHistory;
import com.danawa.watcherApi.database.service.WatcherService;

@Controller 	
@RequestMapping("jChar")
public class JCharController {
	
	@Autowired 
	WatcherService watcherService;
	
	@RequestMapping(value ="/view.png" , method = RequestMethod.GET)
	public void versionCheck(@RequestParam(value = "baseProductSeq", required = true ) int baseProductSeq,
			@RequestParam(value = "inflowProductSeq", required = true ) int inflowProductSeq,
			@RequestParam(value = "width", required = false, defaultValue = "725" ) int width,
			@RequestParam(value = "height", required = false, defaultValue = "440" ) int height,
			@RequestParam(value = "month", required = false, defaultValue = "12" ) int month,
			ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws Throwable{	
		
		PriceHistory priceHistory = new PriceHistory();
		priceHistory.setOperator(Operator.EQUAL, "inflowProductSeq", inflowProductSeq);
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, -month); 
	    
		priceHistory.setCondition(Condition.BETWEEN, "inputDateTime", new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()), new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		
		List<PriceHistory> data = watcherService.getPriceHistories(priceHistory);
		
		BaseProduct baseProduct = watcherService.getBaseProduct(baseProductSeq);
		
		JChartUtil.displayChart(baseProduct.getName(),width, height,data, response);
	}
}
